//
//  MyFootballPointTool.m
//  LiveSoftware
//
//  Created by lanou3g on 15/7/22.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MyFootballPointTool.h"


#import "ScoreModel.h"

@interface MyFootballPointTool()

@property(nonatomic,strong)NSArray *checkAgainArray;

@property(nonatomic)BOOL isAgain;

@end

@implementation MyFootballPointTool


singleton_implementation(MyFootballPointTool);

static  int  indexOfSong;


static FMDatabase *_db;



-(void)creatTable
{
    //1.打开数据库
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"myPoint.sqlite"];
    
    NSLog(@"----path历史记录-----%@-------",path);
    
    _db = [FMDatabase databaseWithPath:path];
    [_db open];
    
    
    //2.创表
    BOOL  flag =   [_db executeUpdate:@"CREATE TABLE IF NOT EXISTS Football (id integer PRIMARY KEY,startTime text,leagueName text , matchState text,curentTime text,    homeName text,homeWholeGoals text,homeRedCard text ,homeYellowCard text,           awayName text ,awayWholeGoals text, awayRedCard text,awayYellowCard text,  matchId text, isSelectPoint text );"];
    
    
    NSLog(@"创表---%d",flag);
    
}




-(void)addDataWithModel:(ScoreModel*)model
{
    //首先取出现有的歌曲
    
    self.checkAgainArray = [NSMutableArray array];
    self. checkAgainArray  = [self getModels];
    
    if(self.checkAgainArray.count != 0)
    {
        self.isAgain=NO;
        indexOfSong = 0;
        for (int i  =0; i < self.checkAgainArray.count; i ++)
        {
            if ([model.matchId  isEqualToString:[self.checkAgainArray[i]matchId]  ])
            {
                self.isAgain =YES;
                indexOfSong = i;
                break;
            }
            
        }
        
        
        if(self.isAgain ==YES)//思路如下:如果有相等的,先把原来的给删除,然后再添加进去
        {
            ScoreModel*musicModel = self.checkAgainArray[indexOfSong];
            
            [self deleteModel:musicModel];
            
            
            
            
           [_db  executeUpdate:@"INSERT INTO Football(startTime ,leagueName  , matchState ,curentTime ,    homeName ,homeWholeGoals ,homeRedCard  ,homeYellowCard ,      awayName  ,awayWholeGoals , awayRedCard ,awayYellowCard ,  matchId , isSelectPoint ) VALUES (?, ?,?, ?,?,?,?,?,?,?,?,?,?,?);", model.startTime,model.leagueName, model.matchState ,model.curentTime,    model.homeName ,model.homeWholeGoals ,model.homeRedCard,model.homeYellowCard ,     model. awayName ,model.awayWholeGoals , model.awayRedCard,model.awayYellowCard ,  model.matchId ,[NSString stringWithFormat:@"%d",model.isSelectPoint ] ];
            
            
        }
        else
        {
            
            
            BOOL addFlag =[_db  executeUpdate:@"INSERT INTO Football(startTime ,leagueName  , matchState ,curentTime ,    homeName ,homeWholeGoals ,homeRedCard  ,homeYellowCard ,      awayName  ,awayWholeGoals , awayRedCard ,awayYellowCard ,  matchId , isSelectPoint ) VALUES (?, ?,?, ?,?,?,?,?,?,?,?,?,?,?);", model.startTime,model.leagueName, model.matchState ,model.curentTime,    model.homeName ,model.homeWholeGoals ,model.homeRedCard,model.homeYellowCard ,     model. awayName ,model.awayWholeGoals , model.awayRedCard,model.awayYellowCard ,  model.matchId ,[NSString stringWithFormat:@"%d",model.isSelectPoint ] ];
        }
        
        
    }
    
    else
    {
        
        
        
        
       [_db  executeUpdate:@"INSERT INTO Football(startTime ,leagueName  , matchState ,curentTime ,    homeName ,homeWholeGoals ,homeRedCard  ,homeYellowCard ,      awayName  ,awayWholeGoals , awayRedCard ,awayYellowCard ,  matchId , isSelectPoint ) VALUES (?, ?,?, ?,?,?,?,?,?,?,?,?,?,?);", model.startTime,model.leagueName, model.matchState ,model.curentTime,    model.homeName ,model.homeWholeGoals ,model.homeRedCard,model.homeYellowCard ,     model. awayName ,model.awayWholeGoals , model.awayRedCard,model.awayYellowCard ,  model.matchId ,[NSString stringWithFormat:@"%d",model.isSelectPoint ] ];
        
    }
    
}

//取出
-(NSArray *)getModels
{
    //得到结果集
    FMResultSet *set = [_db  executeQuery:@"SELECT * FROM Football;"];
    
    
    //不段的往下取数据
    NSMutableArray *modesArray = [NSMutableArray array];
    
    while (set.next) {
        
        
        
        ScoreModel *model = [[ScoreModel alloc]init];
        
        model.startTime= [set  stringForColumn:@"startTime"];

        model.leagueName= [set  stringForColumn:@"leagueName"];

        model.matchState= [set  stringForColumn:@"matchState"];

        model.curentTime= [set  stringForColumn:@"curentTime"];
        model.homeName = [set  stringForColumn:@"homeName"];

        model.homeWholeGoals= [set  stringForColumn:@"homeWholeGoals"];

        model.homeRedCard= [set  stringForColumn:@"homeRedCard"];

        model.homeYellowCard = [set  stringForColumn:@"homeYellowCard"];

        model. awayName = [set  stringForColumn:@"awayName"];

        model.awayWholeGoals = [set  stringForColumn:@"awayWholeGoals"];

        model.awayRedCard= [set  stringForColumn:@"awayRedCard"];

        model.awayYellowCard = [set  stringForColumn:@"awayYellowCard"];

        model.matchId = [set  stringForColumn:@"matchId"];

      
        
         model.isSelectPoint = [[set stringForColumn: @"isSelectPoint"]boolValue];
        
        
        [modesArray addObject:model];
        
    }
    
    return modesArray;
    
}


//删除

-(void)deleteModel:(ScoreModel*)model
{
    
 [_db  executeUpdate:@"delete from Football  where  matchId  = ?", model.matchId];
    
    
}



@end
